package com.rzico.sbl.ui.manager

import com.lzg.extend.jackson.JsonUtil
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCouponDetailBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.CouponData
import com.rzico.sbl.viewmodel.CouponViewModel
import com.xinnuo.common.extend.formatDecimalWhenHave
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.visibleValue
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.BaseActivity

class CouponDetailActivity : BaseActivity(R.layout.activity_coupon_detail) {

    private val mBinding by lazy { ActivityCouponDetailBinding.bind(getRootView()) }

    override fun getViewModel() = get<CouponViewModel>()

    override fun initData() {
        initTitle(title = "优惠券详情")

        getData()
    }

    /**
     * 优惠券详情
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .request(
                requestUrl = BaseUrl.mngCouponDet,
                requestMethod = HttpMethod.GET,
                params = arrayOf("id" to intent.getExtra("itemId")),
                onSuccess = {
                    initDisplay(JsonUtil.jsonToBean(it, CouponData::class.java))
                }
            )
    }

    private fun initDisplay(item: CouponData?) {
        item?.let {
            mBinding.apply {
                couponTitle.text = "${
                    when (it.scope) {
                        "all" -> "全场"
                        "shop" -> "店内"
                        "mall" -> "商城"
                        else -> ""
                    }
                }${
                    when (it.type) {
                        "fullcut" -> "满减优惠券"
                        "discount" -> "满折优惠券"
                        "product" -> "商品券"
                        else -> ""
                    }
                }"

                when (it.type) {
                    "fullcut" -> couponImg.setBackgroundResource(R.mipmap.icon_manager17)
                    "discount" -> couponImg.setBackgroundResource(R.mipmap.icon_manager18)
                    "product" -> couponImg.setBackgroundResource(R.mipmap.icon_manager19)
                }

                couponType.text = "满${it.minimumPrice.formatDecimalWhenHave()}元可用"
                couponCount.text = it.amount.trimEnd('元')
                couponRange.text =
                    "有效期：${it.beginDate.getTimeString("yyyy.MM.dd")} ~ ${
                        it.endDate.getTimeString("yyyy.MM.dd")
                    }"

                couponHint1.visibility = visibleValue { it.type == "fullcut" }
                couponHint2.visibility = visibleValue { it.type == "discount" }

                couponHint3.text =
                    when (it.type) {
                        "fullcut" -> "满减"
                        "discount" -> "满折"
                        else -> ""
                    }
                couponHint4.text =
                    when (it.scope) {
                        "all" -> "全场"
                        "shop" -> "店内"
                        "mall" -> "商城"
                        else -> ""
                    }
                couponHint5.text = "${it.stock}张"
                couponHint6.text = it.amount
                couponHint7.text = "满${it.minimumPrice.formatDecimalWhenHave()}元可用"
                couponHint8.text =
                    "${it.beginDate.getTimeString("yyyy.MM.dd")} ~ ${
                        it.endDate.getTimeString("yyyy.MM.dd")
                    }"
                couponHint9.text = it.introduction
            }
        }
    }

}